const {app, BrowserWindow}  = require('electron')
const path = require('path')
const WinState = require('electron-win-state').default
const createTray = require('./tray')

const createWindow = ()=>{
const winState = new WinState({
    defaultWidth: 800,
    defaultHeight: 600
})
    const win = new BrowserWindow({
        //自定义窗口状态
        ...winState.winOptions,
        webPreferences:{
        //解析preload下的js路径
            preload: path.resolve(__dirname, './preload/index.js')
        },
        icon: path.join(__dirname, "./build/icons/128x128.png"),
        show: false,
        webPreferences: {
            nodeIntegration: true,
            contextIsolation:false  //  把这一项加上错误就会消失
         }
        
    })
    
    
    // win.webContents.openDevTools()

    win.loadURL('http://121.4.71.94/addressbook')
   // win.loadURL('http://127.0.0.1:5173')
    createTray(app,win)

    winState.manage(win)

    win.on('ready-to-show',() => {
        win.show()
    })
}


app.whenReady().then(() => {
    createWindow()

    app.on('activate',() => {
        if(BrowserWindow.getAllWindows().length===0) createWindow();
    })
})

app.on('window-all-closed',() => {
    if(process.platform !== 'darwin')  app.quit()
})